package com.btb.adapter.service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.web3j.abi.EventEncoder;
import org.web3j.abi.datatypes.Event;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.core.methods.request.EthFilter;
import org.web3j.protocol.core.methods.response.EthGetTransactionReceipt;
import org.web3j.protocol.core.methods.response.EthLog;
import org.web3j.protocol.core.methods.response.Log;
import org.web3j.protocol.core.methods.response.TransactionReceipt;

import com.btb.adapter.utils.Web3jUtils;
import com.btb.adapter.vo.TransactionReceiptEx;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class ChainblockEventService {
	@Autowired
	private Web3j web3j;

	public List<TransactionReceiptEx> event(String contractBuyAddress, Event event, String startBlockNumber,
			String endBlockNumber) {
		List<TransactionReceiptEx> list = new ArrayList<>();
		EthFilter filter = new EthFilter(DefaultBlockParameter.valueOf(new BigInteger(startBlockNumber)),
				DefaultBlockParameter.valueOf(new BigInteger(endBlockNumber)), contractBuyAddress)
						.addSingleTopic(EventEncoder.encode(event));
		EthLog ethLog = null;
		try {
			ethLog = web3j.ethGetLogs(filter).send();
		} catch (Exception e) {
			log.error("contractExtract err: {}", e.getMessage(), e);
			throw new IllegalStateException(e.getMessage());
		}
		if (Objects.isNull(ethLog)) {
			return null;
		}
		ethLog.getLogs().forEach(r->{
			Log logs = (Log) r;
			Request<?, EthGetTransactionReceipt> ethGetTransactionReceipt = web3j
					.ethGetTransactionReceipt(logs.getTransactionHash());
			try {
				EthGetTransactionReceipt send = ethGetTransactionReceipt.send();
				if(Objects.nonNull(send)) {
					TransactionReceipt result = send.getResult();
					final String timestamp = Web3jUtils.queryTimestamp(web3j, result.getBlockNumber());
					TransactionReceiptEx build = TransactionReceiptEx.builder()
						.data(logs.getData())
						.timestamp(timestamp)
						.transactionReceipt(result)
						.build();
					list.add(build);
				}
			} catch (Exception e) {
				log.error("TransferRecord query err: {}", e.getMessage(), e);
				throw new IllegalStateException(e.getMessage());
			}
		});
		return list;
	}
	public boolean query(String transactionHash) {
		Request<?, EthGetTransactionReceipt> ethGetTransactionReceipt = web3j
				.ethGetTransactionReceipt(transactionHash);
		try {
			EthGetTransactionReceipt send = ethGetTransactionReceipt.send();
			if(Objects.nonNull(send)) {
				TransactionReceipt result = send.getResult();
				return result.isStatusOK();
			}
		} catch (Exception e) {
			log.error("TransferRecord query err: {}", e.getMessage(), e);
			throw new IllegalStateException(e.getMessage());
		}
		return false;
	}


}
